package io.renren.modules.sys.controller;

import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import io.renren.common.utils.*;
import io.renren.modules.sys.bottom.ExtractStatusEnum;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.SysExtractEntity;
import io.renren.modules.sys.service.SysExtractService;


/**
 * 用户提现记录
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-11-08 19:36:41
 */
@RestController
@RequestMapping("/sys/sysextract")
public class SysExtractController extends AbstractController{

	@Autowired
	private SysExtractService sysExtractService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:sysextract:list")
	public R list(@RequestParam Map<String, Object> params){

		if (getUserId() != Constant.SUPER_ADMIN && hasRole((long)Constant.NORMAL_USER)) {
			params.put("userId", getUserId());
		}

		//查询列表数据
        Query query = new Query(params);

		List<SysExtractEntity> sysExtractList = sysExtractService.queryList(query);
		int total = sysExtractService.queryTotal(query);

		List<Map<String, Object>> list = createStatusList(sysExtractList);

		PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	private List<Map<String, Object>> createStatusList(List<SysExtractEntity> sysExtractList) {
		if (CollectionUtils.isEmpty(sysExtractList)) {
			return null;
		}
		List<Map<String, Object>> result = Lists.newArrayListWithCapacity(sysExtractList.size());
		for (SysExtractEntity sysExtractEntity : sysExtractList) {
			Map<String, Object> model = ObjectUtils.objectToMap(sysExtractEntity);
			ExtractStatusEnum extractStatusEnum = ExtractStatusEnum.getBycode(sysExtractEntity.getStatus());
			model.put("status", extractStatusEnum == null ? "其他" : extractStatusEnum.getMessage());

			result.add(model);
		}
		return result;
	}
	/**
	 * 信息
	 */
	@RequestMapping("/info/{extractId}")
	@RequiresPermissions("sys:sysextract:info")
	public R info(@PathVariable("extractId") Long extractId){
		SysExtractEntity sysExtract = sysExtractService.queryObject(extractId);
		
		return R.ok().put("sysExtract", sysExtract);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:sysextract:save")
	public R save(String payPassword, Long amount, String bankCard, String realName, String openBank){

        if (!StringUtils.equals(ShiroUtils.getUserEntity().getPayPassword(), payPassword)) {
            return R.error("支付密码不正确！");
        }
        if (amount == null || amount <= 0) {
            return R.error("请输入正确的提现金额！");
        }
        if (ShiroUtils.getUserEntity().getBalance() < amount * 100) {
            return R.error("您的余额不足！");
        }
		if (StringUtils.isBlank(bankCard)) {
        	return R.error("请输入银行卡号！");
		}
		if (StringUtils.isBlank(realName)) {
        	return R.error("请输入用户名！");
		}
		if (StringUtils.isBlank(openBank)) {
        	return R.error("请输入开户行！");
		}

		SysExtractEntity sysExtract = new SysExtractEntity();
        sysExtract.setAmount(amount);
        sysExtract.setUserId(ShiroUtils.getUserId());
        sysExtract.setCreatePerson(ShiroUtils.getUserId());
        sysExtract.setWeixin(ShiroUtils.getUserEntity().getWeixin());
        sysExtract.setStatus(ExtractStatusEnum.WAITING.getCode());
        sysExtract.setRealName(realName);
        sysExtract.setBankCard(bankCard);
        sysExtract.setOpenBank(openBank);

		sysExtractService.save(sysExtract);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:sysextract:update")
	public R update(@RequestBody SysExtractEntity sysExtract){

	    if (ExtractStatusEnum.SUCCESS.getCode() == sysExtract.getStatus()) {
            SysExtractEntity queryResult = sysExtractService.queryObject(sysExtract.getExtractId());
            if (queryResult == null) {
                return R.error("该记录已删除或不存在！");
            }
            if (ExtractStatusEnum.WAITING.getCode() != queryResult.getStatus()) {
                return R.error("该记录无法提现");
            }
			sysExtract.setUserId(queryResult.getUserId());
            sysExtract.setAmount(queryResult.getAmount());
            sysExtract.setUpdatePerson(ShiroUtils.getUserId());
        }

		sysExtractService.update(sysExtract);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:sysextract:delete")
	public R delete(@RequestBody Long[] extractIds){
		sysExtractService.deleteBatch(extractIds);
		
		return R.ok();
	}
	
}
